package vn.gq.udv.utils.download;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.zip.GZIPInputStream;
import javax.net.ssl.HttpsURLConnection;
import vn.gq.udv.utils.Base64;
import vn.gq.udv.utils.IOHelper;
import vn.gq.udv.utils.IProgressStatus;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Base64InputStream;
import android.util.Log;

public class SimpleDownload extends AsyncTask<String, Integer, Void> {

	public static final String TAG = SimpleDownload.class.getSimpleName();
	public static final int PROGRESS_DIALOG_STYLE = 0;
	
	private String downloadPath;
	private DataType dataType;
	private ProgressDialog mProgressDialog;
	private int resultCode = 0;
	private Context context;
	
	public SimpleDownload(Context context) {
		this.context = context;
	}
	
	public SimpleDownload(Context context, ProgressDialog dialog) {
		this(context);
		setProgressDialog(dialog);
	}
	
	public void setProgressDialog(ProgressDialog dialog) {
		mProgressDialog = dialog;
	}
	
	public void download(String url, String downloadPath, IClientListener listener) {
		download(url, downloadPath, DataType.NORMAL, listener);
	}
	
	public void download(String url, String downloadPath, DataType gzip, IClientListener listener) {
		this.clientListener = listener;
		this.downloadPath = downloadPath;
		dataType = gzip;
		execute(url);
	}
	
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		if (mProgressDialog != null)
			mProgressDialog.show();
	}

	@Override
	protected Void doInBackground(String... urls) {
		String url = urls[0];
		HttpsURLConnection connection = null;
		try {
			Log.i(TAG, "-----------------------start downloading url "+url);
			connection = (HttpsURLConnection) new URL(url).openConnection();
			connection.setRequestMethod("GET");
			connection.connect();
			int resultCode = connection.getResponseCode();
			if (resultCode == 200) {
				final int contentLength = connection.getContentLength();
				InputStream is = null;
				switch (dataType) {
					case GZIP:
						is = new GZIPInputStream(connection.getInputStream());
						break;
					case BASE64GZIP:
						is = new Base64InputStream(new GZIPInputStream(connection.getInputStream()), Base64.URL_SAFE);
						break;
					default:
						is = connection.getInputStream();
				}
				IOHelper.writeFile(is, downloadPath, new IProgressStatus() {
					
					@Override
					public void onPostProgress(int value) {
						publishProgress((int) value*100/contentLength);
					}
				});
			}
			connection.disconnect();
		} catch (MalformedURLException e) {
			resultCode = 1;
			e.printStackTrace();
		} catch (IOException e) {
			resultCode = 2;
			e.printStackTrace();
		}
		return null;
	}

	@Override
	protected void onProgressUpdate(Integer... values) {
		if (mProgressDialog != null)
			mProgressDialog.setProgress(values[0]);
	}

	@Override
	protected void onPostExecute(Void result) {
		if (mProgressDialog != null)
			mProgressDialog.dismiss();
		if (clientListener != null) {
			if (resultCode != 0) downloadPath = "";
			clientListener.onDone(downloadPath, resultCode);
		}
	}
	
	private IClientListener clientListener;
	
	public interface IClientListener {
		void onDone(String filePath, int resultCode);
	}
	
	
}
